﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace FormDesigner
{
    public partial class MainPage : UserControl
    {

        public MainPage()
        {
            InitializeComponent();

            t.InitTable();
            //t.panel.Margin = new Thickness(5);
            //Canvas.SetLeft(t, 105);
            //this.LayoutRoot.Children.Add(t);

            t.CtrlSelectedEvent += t_CtrlSelectedEvent;

            int oa_tableID = 0;
            try
            {
                if (HtmlPage.Document.QueryString != null && HtmlPage.Document.QueryString.ContainsKey("id"))
                    int.TryParse(HtmlPage.Document.QueryString["id"], out oa_tableID);
            }
            catch (Exception err)
            {
            }
            if (oa_tableID > 0)
                t.ReadTableData(oa_tableID);
            else
            {
                t.RefreshModel();
                t.Refresh();
            }
        }

        private void btnInit_Click(object sender, RoutedEventArgs e)
        {
            t.panel.Children.Clear();
            t.InitTable();
            //t.panel.Margin = new Thickness(5);
            Canvas.SetLeft(t, 105);
            this.LayoutRoot.Children.Add(t);
            t.RefreshModel();
            t.Refresh();

            t.CtrlSelectedEvent += t_CtrlSelectedEvent;
        }

        void t_CtrlSelectedEvent(object sender, EventArgs e)
        {
            Control ctrl = sender as Control;
            if (ctrl != null && ctrl.Tag != null)
            {
                //propertyGrid.SelectedObject = null;
                propertyGrid.SelectedObject = ctrl.Tag;
            }

        }


        private void btnAddRow_Click(object sender, RoutedEventArgs e)
        {
            t.AddRow();
        }

        private void btnAddColumn_Click(object sender, RoutedEventArgs e)
        {
            t.AddColumn();
        }

        private void btnMerge_Click(object sender, RoutedEventArgs e)
        {
            t.MergeCell();
        }

        private void btnOutHtml_Click(object sender, RoutedEventArgs e)
        {
            string s = t.OutHtml();
        }

        private void btnRevoc_Click(object sender, RoutedEventArgs e)
        {
            t.Reversion();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            t.SaveTableData();
        }

        private void btnRead_Click(object sender, RoutedEventArgs e)
        {
            t.ReadTableData(10500);
        }

        private void labelTemplate_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Label lab = new Label();
            lab.Content = "文本";
            //lab.Width = 60;
            //lab.Height = 20;
            HtmlLabelModel hlab = new HtmlLabelModel(lab);
            lab.Tag = hlab;
            propertyGrid.SelectedObject = lab.Tag;

            t.AddHtmlControl(lab);
        }

        private void textBoxTemplate_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Label textBox = new Label();
            textBox.Content = "";
            textBox.Width = 90;
            textBox.Height = 20;
            textBox.BorderThickness = new Thickness(1);
            textBox.Background = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
            textBox.BorderBrush = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));

            HtmlTextBoxModel htextBox = new HtmlTextBoxModel(textBox);
            textBox.Tag = htextBox;
            propertyGrid.SelectedObject = textBox.Tag;

            t.AddHtmlControl(textBox);
        }

        private void chekcboxTemplate_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            CheckBox checkbox = new CheckBox();
            checkbox.Content = "复选框";

            HtmlCheckBoxModel hcheckbox = new HtmlCheckBoxModel(checkbox);
            checkbox.Tag = hcheckbox;
            propertyGrid.SelectedObject = checkbox.Tag;

            t.AddHtmlControl(checkbox);
        }

        private void radioTemplate_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            RadioButton radiobtn = new RadioButton();
            radiobtn.Content = "单选框";

            HtmlRadioButtonModel hradiobtn = new HtmlRadioButtonModel(radiobtn);
            radiobtn.Tag = hradiobtn;
            propertyGrid.SelectedObject = radiobtn.Tag;

            t.AddHtmlControl(radiobtn);
        }

        private void frm_KeyDown(object sender, KeyEventArgs e)
        {
            dynamic ctrlModel = this.propertyGrid.SelectedObject;
            if (ctrlModel != null)
            {
                Control select = ctrlModel.ctrl as Control;
                if (select == null) return;

                if (e.Key == Key.Left)
                {
                    Canvas.SetLeft(select, Canvas.GetLeft(select) - 1);
                    t_CtrlSelectedEvent(select, null);
                }
                else if (e.Key == Key.Right)
                {
                    Canvas.SetLeft(select, Canvas.GetLeft(select) + 1);
                    t_CtrlSelectedEvent(select, null);
                }
                else if (e.Key == Key.Up)
                {
                    Canvas.SetTop(select, Canvas.GetTop(select) - 1);
                    t_CtrlSelectedEvent(select, null);
                }
                else if (e.Key == Key.Down)
                {
                    Canvas.SetTop(select, Canvas.GetTop(select) + 1);
                    t_CtrlSelectedEvent(select, null);
                }
                else if (e.Key == Key.Delete)
                {
                    t.panel.Children.Remove(select);
                    this.propertyGrid.SelectedObject = null;
                }
            }
        }

    }
}
